#coding:utf8
from kcalerter.dbconnmysql import connDB, exeUpdate, connClose
from kcalerter.alerLogger import mylogg



#根据预警规则查询需报警的采集内容
'''
def searchContentCollectNoflag():
    querysql="select * from spider_content_collect where flag=0 "
    try:
            conn,cur=connDB()
            cur.execute(querysql);
            resultvalue =cur.fetchall()  
            print(resultvalue);
    except Exception:
            print("查询失败");
            raise;
    finally:
            connClose(conn, cur)
    return resultvalue
#修改处理过的新闻内容状态
def updateContentCollectNoflag(ids):
    print(ids)
    if ids.strip():
        updatesql="update spider_content_collect set flag=1,alert=1 where id in("+ids+")"
        try:
            conn,cur=connDB()
            result= exeUpdate(conn, cur, updatesql)
            print(result);
        except Exception:
            print("更新失败");
            raise;
        finally:
            connClose(conn, cur)             
    return result
#未修改过的采集内容id
def getContentCollectid(Collectlist):
    resultreturnids=''
    for item in Collectlist:
        ids='%d'%item[0]
        if not resultreturnids.strip(): 
            resultreturnids=ids
        else:
            if ids not in resultreturnids:
                resultreturnids=resultreturnids+","+ids
    return resultreturnids
'''
#根据任务pid将处理过的采集新闻内容修改为已处理状态
def updateCollectNoflagbyArletRule(stringtaskid):
    contentflag=''
    for uu in stringtaskid.strip().split(","):
        if uu==0:
            flagaa=0
            break
        else:
            if not contentflag.strip():
                contentflag=uu
            else:
                contentflag=contentflag+","+uu
            flagaa=1
    if flagaa==1:
        updatesql="update spider_content_collect set flag=1,alert=1 where flag=0 and task_id  in("+contentflag+")"
    else:
        updatesql="update spider_content_collect set flag=1,alert=1 where task_id in("+contentflag+")"
    try:
        conn,cur=connDB()
        result= exeUpdate(conn, cur, updatesql)
        print(result);
    except Exception:
        print("更新失败");
        raise;
    finally:
        connClose(conn, cur)  
    return result   
#根据taskid查询未报警未处理的采集内容
def getContentBytaskid(stringtaskid):
    contentflag=''
    for line in stringtaskid.strip().split(","):
        if line==0:
            flagaa=0
            break
        else:
            if not contentflag.strip():
                contentflag=line
            else:
                contentflag=contentflag+","+line
            flagaa=1
    if flagaa==1:
        sql="select * from spider_content_collect where flag=0 and task_id in("+contentflag+")"
        updatesql="update spider_content_collect set flag=1,alert=1 where flag=0 and task_id not in("+contentflag+")"
    else:
        sql="select * from spider_content_collect where flag=0"
    try:
            conn,cur=connDB()
            if flagaa==1:
                exeUpdate(conn, cur, updatesql)
            cur.execute(sql);
            resultvalue =cur.fetchall()  
            print(resultvalue);
    except Exception,e:
            print("通过任务id查询未报警未处理过的采集内容失败"+e);
            mylogg("query collection content by taskid failure"+e)
            raise;
    finally:
            connClose(conn, cur)
    return resultvalue